package com.ptadmin.report.executor;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.ptadmin.report.converter.ReportConverterBase;
import com.ptadmin.report.converter.ReportConverterTabDelimited;
import com.ptadmin.report.converter.ReportDecorator;
import com.ptadmin.report.executor.processor.BrowseTreeReportMongoProcessor;
import com.ptadmin.report.executor.processor.BrowseTreeReportPGProcessor;
import com.ptadmin.report.executor.processor.BrowseTreeReportPGProcessor2;
import com.ptadmin.report.executor.processor.DepartmentPGProcessor;
import com.ptadmin.report.executor.processor.ProcessReportsParameter;
import com.ptadmin.report.output.FlatFileOutput;
import com.ptadmin.report.output.MongoDbOutput;
import com.ptadmin.report.util.Filename;

// input: report file path resides on the local disk
// output: write to different dest based on different report type
public class ProcessReportsExecutor {
	
	private List<ProcessReportsParameter> parameters;

	protected ProcessReportsExecutor(Builder builder) {
		this.parameters = new ArrayList<ProcessReportsParameter>();
		
		for (int i = 0; i < builder.merchantIdList.size(); i++) {
			ProcessReportsParameter p = new ProcessReportsParameter();
			p.setReportName(builder.reportNameList.get(i));
			p.setMerchantId(builder.merchantIdList.get(i));
			String reportFilePath = builder.reportFilePathList.get(i);
			
			String[] temp = reportFilePath.split("_");
			if (temp.length != 3) {
				throw new RuntimeException(String.format("input reportFilePath might invalid, please check.. ", reportFilePath));
			}
			String reportCategory = temp[0];
			p.setReportCategory(reportCategory);
			System.out.println(String.format("report category is %s...", reportCategory));
			p.setReportFilePath(builder.reportFilePathList.get(i));
			System.out.println(String.format("report file path is %s...", reportFilePath));
			this.parameters.add(p);
		}
	}

	public static ProcessReportsExecutor build() {
		return null; // to be implemented
	}

	public static class Builder {
		private List<String> reportNameList = new ArrayList<String>();
		private List<String> reportFilePathList = new ArrayList<String>();
		private List<String> merchantIdList = new ArrayList<String>();
		
		public ProcessReportsExecutor build() {
			ProcessReportsExecutor executor = new ProcessReportsExecutor(this);
			return executor;
		}
		
		public Builder withReportName(String reportName) {
			this.reportNameList.add(reportName);
			return this;
		}
		
		public Builder withReportFilePath(String reportFilePath) {
			this.reportFilePathList.add(reportFilePath);
			return this;
		}
		
		public Builder withMerchantId(String merchantId) {
			this.merchantIdList.add(merchantId);
			return this;
		}
	}
	
	public void execute() {
		for (ProcessReportsParameter p : parameters) {
			System.out.println(String.format("report category is %s...", p.getReportCategory()));
			if ("BrowseTreeNode".equals(p.getReportCategory())) {
				BrowseTreeReportPGProcessor2 processor_pg = new BrowseTreeReportPGProcessor2();
				processor_pg.process(p);
				processor_pg = null;
//				BrowseTreeReportMongoProcessor processor_mongo = new BrowseTreeReportMongoProcessor();
//				processor_mongo.process(p);
//				processor_mongo = null;
			} else if ("department".equals(p.getReportCategory())) {
				DepartmentPGProcessor processor = new DepartmentPGProcessor();
				processor.process(p);
			} else {
				DepartmentPGProcessor processor = new DepartmentPGProcessor();
				processor.process(p);
			}
		}
	}
	
	public static void main(String... args) {
		ProcessReportsExecutor.Builder builder = new ProcessReportsExecutor.Builder();
		builder.withReportName("InventoryA");
		builder.withMerchantId("tttttt");
		builder.withReportFilePath("Invetory_13446768272017930Tab_Delimited.txt");
		builder.withReportName("InventoryB");
		builder.withMerchantId("aaaaaa");
		builder.withReportFilePath("Invetory_13448949893017930Tab_Delimited.txt");
		builder.build().execute();
//		ProcessReportsExecutor executor = new ProcessReportsExecutor();
//		executor.execute("report.xml");
	}
}
